//
//  WarLayer.cpp
//  DiceWar
//
//  Created by Xiao  Feng on 19/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "WarLayer.h"
#include "GameMaster.h"
#include "HexMapLayer.h"
#include "FightingLayer.h"
#include "FightingAnimLayer.h"
#include "PlayerInfoLayer.h"
#include "GameUtil.h"

USING_NS_CC;

WarLayer::WarLayer()
: CCLayer()
, m_pMapLayer(NULL)
, m_pFightingLayer(NULL)
, m_pFightingAnimLayer(NULL)
, m_pPlayerInfoLayer(NULL) {}

WarLayer::~WarLayer() {}

bool WarLayer::init()
{
    if (!CCLayer::init()) return false;
    if (!initLayers()) return false;
    if (!initUserInterface()) return false;

    setInitState();
    setIsTouchEnabled(true);
    return true;
}

void WarLayer::onEnter()
{
    CCLayer::onEnter();
    setInitState();
}

bool WarLayer::initLayers()
{
    m_pMapLayer = HexMapLayer::node();
    if (!m_pMapLayer) return false;
    addChild(m_pMapLayer, 0);
    
    m_pFightingLayer = FightingLayer::node();
    if (!m_pFightingLayer) return false;
    addChild(m_pFightingLayer, 1);
    
    m_pFightingAnimLayer = FightingAnimLayer::node();
    if (!m_pFightingAnimLayer) return false;
    addChild(m_pFightingAnimLayer, 4);

    m_pPlayerInfoLayer = PlayerInfoLayer::node();
    if (!m_pPlayerInfoLayer) return false;
    addChild(m_pPlayerInfoLayer, 3);
    
    return true;
}

bool WarLayer::initUserInterface()
{
    // initialize the end turn button
    CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage("CloseNormal.png", "CloseSelected.png",  this, menu_selector(WarLayer::onEndTurnButtonClicked));
    if (!pCloseItem) return false;
    pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
    
    CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
    if (!pMenu) return false;
    pMenu->setPosition(CCPointZero);
    
    addChild(pMenu, 4);
    
    return true;
}

void WarLayer::setInitState()
{
    m_pMapLayer->updateMapView();
    m_state.changeToWaitForAttacker();
    
    m_playersManager.start();
    m_pPlayerInfoLayer->setCurrentPlayer(m_playersManager.getCurrentPlayer());
}

void WarLayer::startFightAnimation()
{   
    //m_pFightingAnimLayer->startFightingAnim(0, 0, 0, 0, this, callfunc_selector(WarLayer::onFightingAnimOver));
    m_pFightingAnimLayer->startFightingAnim(this, callfunc_selector(WarLayer::onFightingAnimOver));
    //onFightingAnimOver();
}

void WarLayer::onFightingAnimOver()
{
    m_state.onFightingAnimOver(*this);
}

void WarLayer::onCellClicked(int coordinateX, int coordinateY)
{
    if (!HexCellCoordinate::isValid(coordinateX, coordinateY)) return;
    m_state.onHexCellClicked(coordinateX, coordinateY, *this);
}

void WarLayer::registerWithTouchDispatcher()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}

void WarLayer::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCLayer::ccTouchesEnded(pTouches, pEvent);
    
    CCTouch* pFirstTouch = getFirstTouchPoint(pTouches);
    if (!pFirstTouch) return;
    
    CCPoint touchPointInView = pFirstTouch->locationInView();
    CCPoint touchPoint = GameUtil::getPointFromLocationInView(touchPointInView);
    
    CCPoint preTouchPointInView = pFirstTouch->previousLocationInView();
    CCPoint preTouchPoint = GameUtil::getPointFromLocationInView(preTouchPoint);
    
    const HexMap& gameMap = GameMaster::instance().getHexMap();
    HexCellCoordinate coordinate = gameMap.getMapGridCoordinate(ccp(touchPoint.x, touchPoint.y));
    onCellClicked(coordinate.getX(), coordinate.getY());
}

void WarLayer::onEndTurnButtonClicked(CCObject* pSender)
{
    m_playersManager.gotoNextPlayer();
    m_pFightingLayer->unsetAttacker();
    m_pFightingLayer->unsetDefender();
    m_state.changeToWaitForAttacker();
    
    m_pPlayerInfoLayer->setCurrentPlayer(m_playersManager.getCurrentPlayer());
}

cocos2d::CCTouch* WarLayer::getFirstTouchPoint(cocos2d::CCSet* pTouches)
{
    CCAssert(pTouches, "NULL Pointer");
    if (!pTouches) return NULL;
    
    const int kNumTouchPoints = pTouches->count();
    CCAssert(kNumTouchPoints >= 1, "Not enough points in Touches set!");
    if (kNumTouchPoints < 1) return NULL;
    
    CCSetIterator iter = pTouches->begin();
    return static_cast<cocos2d::CCTouch*>(*iter);
}
